﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Reporting;
using GPSTrackingWebsiteManager.ClassManager;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class CatalogStatisticsByItemViewReports : ClassManager.SessionManagerPage
    {

        GPSTrackingReports.Catalog_StatisticsByItemView rpt = new GPSTrackingReports.Catalog_StatisticsByItemView();
        Telerik.Reporting.InstanceReportSource ir = new Telerik.Reporting.InstanceReportSource();
        SessionManager objSessionManager;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];
            }
            catch (Exception ex)
            {
                showMessage(ex.Message);
            }

            if(!IsPostBack){
                if (!IsPostBack)
                beginDateSelected.SelectedDate = DateTime.Now.Date;
                endDateSelected.SelectedDate = DateTime.Now.Date;               
            }
        }

        protected void showMessage(string msg)
        {
            RadWindowManager1.RadAlert("<center>" + msg + "</center>", 400, 100, "Estadisticas de Productos", null, null);
        }

        protected void loadReports()
        {
            int companyID = -1;
            if (objSessionManager != null)
                companyID = objSessionManager.CompanyIDSelectedComboBox;

            try
            {
                if (beginDateSelected.SelectedDate != null)
                {
                    if (!isDate(beginDateSelected.SelectedDate.Value.ToString()))
                    {
                        showMessage("No se ha seleccionado la fecha de inicio para la consulta.");
                        ReportViewer1.ReportSource = null;
                        return;
                    }
                }

                if (endDateSelected.SelectedDate != null)
                {
                    if (!isDate(endDateSelected.SelectedDate.Value.ToString()))
                    {
                        showMessage("No se ha seleccionado la fecha de fin para la consulta.");
                        ReportViewer1.ReportSource = null;
                        return;
                    }
                }

                if (beginDateSelected.SelectedDate.Value > endDateSelected.SelectedDate.Value)
                {
                    showMessage("La fecha de inicio no puede ser mayor a la fecha de fin.");
                    ReportViewer1.ReportSource = null;
                    return;
                }

                System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("~/Template/Companies/" + Request.Cookies["Logo"].Value));
                rpt.SetParameters(beginDateSelected.SelectedDate.Value.Date, endDateSelected.SelectedDate.Value.Date, companyID ,image);
                rpt.ReportParameters["PBeginDate"].Value = beginDateSelected.SelectedDate.Value.Date.ToString("dd/MM/yyyy");
                rpt.ReportParameters["PEndDate"].Value = endDateSelected.SelectedDate.Value.Date.ToString("dd/MM/yyyy");
                
                //rpt.SetOptionalCompanyID(companyID);
                ir.ReportDocument = rpt;
                ReportViewer1.ReportSource = ir;
                ReportViewer1.RefreshReport();

            }
            catch (Exception ex)
            {
                showMessage(ex.Message);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            loadReports();
        }

        public bool isDate(string date)
        {
            try
            {
                DateTime.Parse(date);
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}